package com.zenith.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.article.mapper.ArtTagMapper;
import com.zenith.article.po.domain.ArtTag;
import com.zenith.article.po.dto.ArtTagDTO;
import com.zenith.article.service.ArtTagService;
import com.zenith.common.result.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 文章标签服务实现类
 *
 * @author zenith
 * @since 2025-08-29
 */
@Service
public class ArtTagServiceImpl extends ServiceImpl<ArtTagMapper, ArtTag> implements ArtTagService {

    @Override
    public PageResult<ArtTag> getArtTagPage(ArtTagDTO query) {
        // 构造分页对象
        Page<ArtTag> page = new Page<>(query.getPageNum(), query.getPageSize());
        
        // 构造查询条件
        LambdaQueryWrapper<ArtTag> wrapper = new LambdaQueryWrapper<>();
        if (query.getName() != null && !query.getName().isEmpty()) {
            wrapper.like(ArtTag::getName, query.getName());
        }
        if (query.getStatus() != null) {
            wrapper.eq(ArtTag::getStatus, query.getStatus());
        }
        
        // 执行分页查询
        page = this.page(page, wrapper);
        
        // 返回分页结果
        return PageResult.ofPage(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords());
    }

    @Override
    public boolean createArtTag(ArtTagDTO tagDTO) {
        ArtTag tag = new ArtTag();
        BeanUtils.copyProperties(tagDTO, tag);
        return this.save(tag);
    }

    @Override
    public boolean updateArtTag(ArtTagDTO tagDTO) {
        ArtTag tag = new ArtTag();
        BeanUtils.copyProperties(tagDTO, tag);
        return this.updateById(tag);
    }

    @Override
    public boolean deleteArtTag(Long id) {
        return this.removeById(id);
    }
}